package com.hbnu.dao.impl;

import com.hbnu.dao.RecordDao;
import com.hbnu.pojo.DormBuild;
import com.hbnu.pojo.Record;
import com.hbnu.pojo.User;
import com.hbnu.service.RecordService;
import com.hbnu.util.DBUtils;

import javax.persistence.criteria.CriteriaBuilder;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class RecordDaoImpl implements RecordDao {
    @Override
    public List<Record> find(String string) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;

        try {
            connection= DBUtils.getConnection();

            preparedStatement=connection.prepareStatement(string);

            resultSet=preparedStatement.executeQuery();

            List<Record> records=new ArrayList<Record>();

            while(resultSet.next()){
                Record record = new Record();
                record.setId(resultSet.getInt("recordId"));
                record.setDate(resultSet.getTimestamp("date"));
                record.setRemark(resultSet.getString("remark"));
                record.setDisabled(resultSet.getInt("recordDisabled"));

                User user = new User();
                user.setId(resultSet.getInt("student_id"));
                user.setCreateUserId(resultSet.getInt("create_user_id"));
                user.setDormBuildId(resultSet.getInt("dormBuildId"));
                user.setDormCode(resultSet.getString("dorm_code"));
                user.setName(resultSet.getString("name"));
                user.setPassword(resultSet.getString("password"));
                user.setRoleId(resultSet.getInt("role_id"));
                user.setSex(resultSet.getString("sex"));
                user.setStuCode(resultSet.getString("stu_code"));
                user.setTel(resultSet.getString("tel"));

                DormBuild build = new DormBuild();
                build.setName(resultSet.getString("buildName"));

                user.setDormBuild(build);
                record.setUser(user);

                records.add(record);
            }
            return records;

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtils.close(resultSet,preparedStatement,connection);
        }

        return null;
    }

    @Override
    public Integer getTotalNum(String string) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;

        try {
            connection= DBUtils.getConnection();

            preparedStatement=connection.prepareStatement(string);

            resultSet=preparedStatement.executeQuery();

            resultSet.next();
            Integer count= resultSet.getInt(1);
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtils.close(resultSet,preparedStatement,connection);
        }

        return null;
    }

    @Override
    public Record findById(int id) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;

        try {
            connection= DBUtils.getConnection();
            String sql="select record.*,record.id recordId,record.disabled recordDisabled,user.* from tb_record record" +
                    " left join tb_user user on user.id=record.student_id " +
                    "where record.id=?";

            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);

            resultSet=preparedStatement.executeQuery();

            while(resultSet.next()){
                Record record = new Record();
                record.setId(resultSet.getInt("recordId"));
                record.setStudent_id(resultSet.getInt("student_id"));
                record.setDate(resultSet.getTimestamp("date"));
                record.setRemark(resultSet.getString("remark"));
                record.setDisabled(resultSet.getInt("recordDisabled"));

                User user = new User();
                user.setId(resultSet.getInt("student_id"));
                user.setCreateUserId(resultSet.getInt("create_user_id"));
                user.setDormBuildId(resultSet.getInt("dormBuildId"));
                user.setDormCode(resultSet.getString("dorm_code"));
                user.setName(resultSet.getString("name"));
                user.setPassword(resultSet.getString("password"));
                user.setRoleId(resultSet.getInt("role_id"));
                user.setSex(resultSet.getString("sex"));
                user.setStuCode(resultSet.getString("stu_code"));
                user.setTel(resultSet.getString("tel"));

                record.setUser(user);
                return record;

            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtils.close(resultSet,preparedStatement,connection);
        }

        return null;
    }

    @Override
    public void update(Record record) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;

        try {
            connection=DBUtils.getConnection();

            String sql="update tb_record set student_id=?,date=?,remark=?,disabled=?" +
                    " where id=?";

            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1,record.getStudent_id());
            preparedStatement.setDate(2,new Date(record.getDate().getTime()+24*3600*1000));
            preparedStatement.setString(3, record.getRemark());
            preparedStatement.setInt(4, record.getDisabled());
            preparedStatement.setInt(5, record.getId());

            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtils.close(resultSet,preparedStatement,connection);
        }
    }

    @Override
    public void save(Record record) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;

        try {
            connection=DBUtils.getConnection();

            String sql="insert into tb_record(student_id,date,remark,disabled) values(?,?,?,?)";

            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1,record.getStudent_id());
            preparedStatement.setDate(2,new Date(record.getDate().getTime()));
            preparedStatement.setString(3, record.getRemark());
            preparedStatement.setInt(4, record.getDisabled());

            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtils.close(null,preparedStatement,connection);
        }
    }
}
